<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 添加分类按钮 -->
      <el-row>
        <el-button type="primary" @click="dialogFormVisible = true">添加分类</el-button>
        <el-dialog title="添加分类" :visible.sync="dialogFormVisible">
          <el-form :model="form" ref="form" :rules="rules">
            <el-form-item prop="username" label="分类名称" :label-width="formLabelWidth">
              <el-input v-model="form.username" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="活动区域" :label-width="formLabelWidth">
              <!-- 提交下拉菜单start -->
              <el-cascader ref="cascader" :options="options" placeholder="请选择商品添加区域" style="width: 100%"
                :props="{ checkStrictly: true }" clearable @change="handleChange"></el-cascader>
              <!-- 提交下拉菜单end-->
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addfun1">确 定</el-button>
          </div>
        </el-dialog>
      </el-row>

      <!-- 树状表格 -->
      <tree-table :data="goodsList" :columns="columns" :selection-type="false" :expand-type="false"
        style="margin-top: 15px" border>
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color: lightgreen" v-if="scope.row.cat_deleted === false"></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <template slot="level" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level == 0">一级 </el-tag>
          <el-tag v-if="scope.row.cat_level == 1">二级 </el-tag>
          <el-tag v-if="scope.row.cat_level == 2">三级 </el-tag>
        </template>
      </tree-table>

      <!-- 分页 -->
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pagesize" layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // strname: '',
      // 表单必填项
      rules: {
        name: [
          { required: true, message: '不能为空', trigger: 'blur' },
        ],
      },
      form: {
        username: '',
      },
      // xia'la
      options: [],
      // 查询参数
      queryInfo: {
        // 当前页码
        pagenum: 1,
        // 默认每页5条数据
        pagesize: 5,
      },
      // 商品分类
      goodsList: [],
      // 总数据条数
      total: 0,
      // 树状表格属性
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name',
        },
        {
          label: '是否有效',
          type: 'template', //将当前列定义为模版列
          template: 'isok', //表示当前这一列使用模版名称
        },
        {
          label: '排序',
          type: 'template', //将当前列定义为模版列
          template: 'level',
        },
      ],
      // 添加数据 按钮的点击事件默认的状态
      dialogFormVisible: false,
      formLabelWidth: '120px',
    }
  },
  //  生命周期
  created() {
    this.classifyList()
    this.getxiala()
  },
  // 事件处理函数
  methods: {
    // 获取商品分类的数据
    async classifyList() {
      const res = await this.$http.get('categories', { params: this.queryInfo })
      // console.log(res.status)
      // console.log(res)
      // if (res.status == 200) {
      //   this.$message.success('获取数据成功')
      //   console.log(res.data.meta.msg)
      // }
      console.log(res)
      // console.log(res.data.data.total)
      this.goodsList = res.data.data.result
      this.total = res.data.data.total
    },
    // 每页多少条数，条数发生变化，页面显示相应条数
    handleSizeChange(val) {
      // 将最新每页数据条数 保存到后台
      this.queryInfo.pagesize = val
      console.log(`每页${val}条`)
      // 获取数据 进行新的渲染 页面显示相应的条数
      this.classifyList()
    },
    // 当前页发生变化 触发该事件处理函数  页面显示当前页所对应的数据
    handleCurrentChange(val) {
      // 最新当前页 保存到 后台
      this.queryInfo.pagenum = val
      console.log(`当前页: ${val}`)
      // 获取数据 进行新的渲染 显示当前页对应的数据在页面
      this.classifyList()
    },
    async getxiala() {
      let res = await this.$http.get('categories', {
        params: {
          type: 2,
          pagenum: 1,
          // 默认每页5条数据
          pagesize: 999999,
        },
      })
      console.log(res)
      // this.options = res.data.data.result
      let obj1 = res.data.data.result
      obj1.forEach((obj1) => {
        obj1.label = obj1.cat_name
        obj1.value = obj1.cat_id
        if (Object.keys(obj1).length == 8) {
          obj1.children.forEach((obj2) => {
            obj2.label = obj2.cat_name
            obj2.value = obj2.cat_id
            obj2.valuecatid = obj2.cat_id
          })
        }
      })
      this.options = obj1
      console.log(obj1)
    },
    handleChange(value) {
      // console.log(this.$refs['cascader'].getCheckedNodes()[0].label)
    },
    async addfun1() {
      if (this.form.username.length == 0) {
        this.$message({
          type: 'error',
          message: '输入不能为空'
        });
        return
      }
      this.dialogFormVisible = false
      // 一级分类添加
      if (this.$refs['cascader'].getCheckedNodes()[0] == undefined) {
        console.log(this.form.username) //名称
        let res = await this.$http({
          url: 'categories',
          method: 'post',
          data: {
            cat_pid: 0,
            cat_name: this.form.username,
            cat_level: 0,
          },
        })
        this.form.username = ''
        this.classifyList()
        this.getxiala()
        return
      }
      // 二级分类添加
      if (this.$refs['cascader'].getCheckedNodes()[0].pathLabels.length == 1) {
        console.log(this.form.username) //名称
        let res = await this.$http({
          url: 'categories',
          method: 'post',
          data: {
            cat_pid: this.$refs['cascader'].getCheckedNodes()[0].data.cat_id, //父类
            cat_name: this.form.username,
            cat_level: 1,
          },
        })
        this.form.username = ''
        this.classifyList()
        this.getxiala()
        return
      }
      if (this.$refs['cascader'].getCheckedNodes()[0].pathLabels.length == 2) {
        console.log(this.form.username) //名称
        let res = await this.$http({
          url: 'categories',
          method: 'post',
          data: {
            cat_pid: this.$refs['cascader'].getCheckedNodes()[0].data.cat_id, //父类
            cat_name: this.form.username,
            cat_level: 2,
          },
        })
        this.form.username = ''
        this.classifyList()
        this.getxiala()
        return
      }
    },
  },
}
</script>

<style lang="less" scoped>

</style>
